All righty.
Now that we know the different loops in Lua, I want to go ahead and have us practice with them a little
bit.
We're also going to be learning about two new keywords in Lua as well.
And these are going to be the break and the continue keywords.
The break keyword allows for us to exit or break out of a loop prematurely.
This keyword is useful for whenever we have a loop that needs to exit when some condition is met.
So to demonstrate this break keyword, what I'm going to do is create an infinite loop.
So while true or we're going to do is create a new scope here.
And I'm going to create a variable outside of this loop, I'm just going to call it I and set it to
the value of zero.
And I'm going to do inside of this while loop is increment I by one.
So we could use the plus equal operator and put our value of one here.
And if the value of AI goes beyond, let's say, the value of ten, then what I want to do is exit out
of this infinite while loop.
And this is where we can use the break keyword.
So when the interpreter encounters this break keyword, it's going to exit out of this while loop and
then continue down into our script.
Because once our interpreter hits this while loop, it's going to be stuck inside of here.
The main thread is going to be stuck inside of here until it has the opportunity to exit.
And when this condition evaluates to true, we are able to hit this break keyword hop out of the while
loop and continue executing down our script.
So this can be useful in many different circumstances when you're trying to script game logic, and
you might have a while loop or a loop that executes forever, and you need to be able to exit out of
that loop when a certain condition is met.
So what I'm also going to put in here is a simple wait statement.
And actually I'll put it up top here so we can like wait for one second.
And then I'm just going to print out I into the console.
And then we can go ahead and run our game.
And as you can see, our loop is executing every single second.
And eventually, once the value exceeds ten, our loop is going to break.
So here's ten and there we go.
We have stopped executing when I was incremented to 11.
That condition evaluated to true.
And we broke out of that loop.
So when I hit ten and then we incremented I here to 11.
We evaluated it inside of here and realized that 11 is greater than ten.
And then we hit this break statement.
And then we exited out of the while loop, meaning we ignored the rest of the code below the break statement.
Pretty simple and very nice to have when you're scripting in loops.
Now the other keyword is the continue keyword, and this one allows us to continue to the next iteration
of the loop and ignore everything else below it.
This keyword is useful for when we have to, let's say, search through a set of data, and we only
want to affect certain data inside of it based on a condition.
Maybe we only want to affect positive numbers or certain data types and so on.
This is where we can evaluate for a condition, and if the condition does not evaluate to true, we
ignore it and continue to the next value using the continue keyword.
So to demonstrate that what I'm going to do is create a table.
I'm going to call it my array.
And I'm just going to store some numbers in here like one, two, three, four and five.
And then we can go ahead and loop through this table.
I do not care about the index only the values.
So I'll create an underline for the index variable and then put v here for the value in ipairs pass
my array.
And what I want to do is I want to, let's say, only print out numbers inside of this table that are
even.
So in order to do that, we have to use an operator in Lua called the modulus operator.
And this is a simple operator where it just basically divides your number by the number you supply.
And if there's any leftovers, that's what gets returned.
So for example we can check if v and then we use the modulus operator which is the percent sign.
And then we can put two.
And we can check to see if this is equal to zero.
If this is equal to zero that means it is even because if the value can be divided by two and has a
leftover value of zero, that means we perfectly fit two inside of our value.
If our value was, let's say three.
Well, we can only divide three by two once, and the leftover value would be one, which means it is
not an even value.
But in this case, we only want to print out values that are even.
So what I'm going to do here is that if this value using the modulus operator by two is not equal to
zero, which means it's an odd number, then we want to ignore this value and go to the next value that's
inside of our table.
So inside of this if statement we can use the continue keyword.
And that means everything below the continue keyword is going to be ignored.
And we're going to move on to the next iteration of our loop.
So down here, all I'm going to do is simply print V into the console.
And then I'm going to go ahead and comment out this code here by using control and then slash to comment
out my entire block here.
So that way we don't have to wait for that loop.
And then we can go ahead and hit run.
And as you can see, only value of two and four was printed out inside of the console because those
were the only even numbers inside of our table.
All the other values were ignored because they were not even numbers, and we moved on to the next iteration
of the loop thanks to our continue keyword.
So with this newly learned knowledge, let's go ahead and practice with a couple of activities I would
like for you to complete.
The first activity is this I want you to create an infinite loop that increments a variable by one in
each iteration.
And I want you to only print out even numbers.
If a number is odd, you're going to use the continue keyword to ignore it, and you're going to use
the modulo or modulus operator to help you out.
So this is going to be your infinite while loop.
That's just going to constantly increment and only print out positive or I mean even values.
Since we just did that up here, this should be a very easy activity for you to complete.
So go ahead, practice doing it right now and then you can go ahead and come back and see my solution.
Alrighty.
So my solution would be this.
We're going to create a variable outside of the while loop, and I'm just going to call it I and set
it equal to zero.
Then we're going to create the while loop.
So while true do here's my infinite while loop.
And inside of here what I want to do is increment I by one.
So plus equal one.
And then I want to check if I modulus two is not equal to zero, which means that this is an odd number.
If it is then we're going to continue and move on to the next iteration of the loop.
Otherwise we can go ahead and simply print out I into the console.
Here we have our infinitely executing loop, and since it is an infinitely executing loop, that means
we must yield.
So at the top I'm just going to add a simple yield function up here.
And we'll just yield for like let's say one second.
Then we can go ahead and run our game.
And as you can see, we get two and then we get four.
And through each iteration of our loop, we're waiting one second, right?
So we're actually waiting two seconds between these different values, because the values that we're
in between them were odd numbers, and because they were odd numbers, we're ignoring them and moving
on to the next value.
So if you don't want to wait two seconds in between values, then what we could do instead.
As we could add our weight statement.
At the bottom.
So this way, whenever we encounter a value that is not even, then we're going to immediately skip
to the next iteration without having to yield for one second.
So if we go ahead and run our game again, this time every single second, now we are printing out an
even number inside of our console.
So great job on that first activity.
We're now going to move on to the next activity I would like you to complete, which is going to be
a little bit more difficult.
So I'm just going to comment out my solution right here.
And let's go ahead and move on to the next activity.
In this activity, I would like for you to create a simple random dungeon game.
I want you to create an infinite loop that generates a random number between 1 and 10.
And you can do so by using the math dot random function to help you.
And I'll demonstrate that in a little bit.
So when the number that is generated by this function is equal to one, I want you to display a string
saying the exit was found.
And then I want you to exit out of the loop using the break keyword.
However, when the number is equal to two, then I want you to display a string in the console that
says the player encountered a monster.
When the number is equal to three, I want you to display a string saying the player encountered some
loop, and then when the number is not equal to any of those previous values, then I want you to simply
display a string saying the player is wandering the dungeon.
So the math dot random function real quick is a very simple function.
It takes a minimum number and a maximum number, and it returns a random number within the range provided.
So if I would like to generate a random number between 1 and 10, then I could simply just pass one
and ten here.
And it's going to pick a random number between those two values.
And of course you can store the return of this function inside of a variable like random int.
And this is what you're going to be using for your infinite while loop.
So go ahead, take a minute or two to create your solution and then come back to see my solution.
All righty.
My solution for this problem is first we need to create our while loop.
So while true do.
And then inside of here we need to generate our random number.
So I can create a variable I'll call it random int.
And this is equal to math dot random.
And the range is going to be from one up to ten.
And now we can go ahead and create our if statement section, so we can check if our random integer
is equal to one.
If it is, then we can print out inside of the console the player found the exit, and since we have
reached or attained the value of one, we want to go ahead and exit out of this forever loop so we can
use the break keyword.
And now we have exited out of our while loop.
Else if our random integer let's say it's equal to two, then we need to print into the console that
we encountered a monster.
So we can print.
The player encountered a monster.
However, if the value.
Is equal to three.
Then what we need to print out is that the player found some loop.
And last but not least, if the value randomly generated here does not match any of these previous conditions,
then we can use the else statement and create a new block down here and we'll just print into the console.
The player is wandering the dungeon.
And since we don't know how long this loop will execute for, I'm also going to add a wait statement
at the end so we can wait for, let's say, one second once more.
And now this should be our solution complete.
So let's go ahead and run our game and see what gets printed into the console.
There we go.
Players wandering.
We've encountered a monster.
We've continued to wander.
And then eventually we found the exit.
So our math dot random function finally generated the number of one while generating three four, five
previous values that were not equal to one.
And of course, we can stop the test and run it again, and this time it'll print out some different
statements.
So this time it executed much shorter.
It only had two previous values, which was the players wandering the dungeon, and then the player
found the exit.
So we can go ahead and stop this again and hit run once more.
And immediately this one got pretty lucky.
We had a 10% chance of finding the exit.
And look at that.
The player found the exit.
Now, if we would like to decrease the odds of our player being able to find the exit, then we can
increase this range to like 100.
And if the value is equal to one, then the player finds the exit.
Otherwise we can do.
Here is we can check if the random integer is greater than or equal to two.
And this random integer is let's say less than or equal to 15.
If that's the case, then we can print out the player has encountered a monster.
Otherwise we can check if our value is greater than 15, and also if our value is, let's say less than
or equal to 30.
If that's the case, then we can print out that the player has found some loot.
Otherwise, if the value randomly generated here does not match any of the previous ones, then we're
just going to say the player is wandering the dungeon.
So now if we go ahead and run our game this time, our loop should last a lot longer.
So as you can see, we are wandering the dungeon.
Eventually we found some loot and were continuing to wander.
We've encountered a monster.
We are continuing to wander.
And we can go ahead and see how long this will eventually take us to find the exit.
But so far, we're still wandering our dungeon.
Alrighty after quite a bit of time.
Maybe that was 2 or 3 minutes.
Our player has finally found the exit because through each iteration of our loop, our player only had
a 1% chance of finding the exit.
So as you can see, a lot of time has elapsed.
The player was wandering the dungeon, finding loot and countering monsters, and eventually we finally
found the exit and we broke out of our infinite loop.
I hope you enjoyed these two simple practice activities and learning about the break and continue keywords.
Keep up the good work and I will see you in the next lecture.